package com.oa.pm.controller;

import com.joneying.common.web.response.Resp;
import com.oa.pm.dto.LearningExperienceSaveDTO;
import com.oa.pm.entity.LearningExperience;
import com.oa.pm.service.ILearningExperienceService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.joneying.common.annotation.CommonController;
import org.springframework.context.annotation.Description;

import org.springframework.web.bind.annotation.RestController;
import com.oa.core.BaseController;

/**
 * @author syb123
 * @since 2019-01-22
 */
@RestController
@RequestMapping("/learningExperience")
@Api(description = "学习经历表模块")
public class LearningExperienceController extends BaseController {

    private final ILearningExperienceService learningExperienceService;

    @Autowired
    public LearningExperienceController(ILearningExperienceService learningExperienceService) {
        this.learningExperienceService = learningExperienceService;
    }

    @PostMapping("")
    @CommonController
    @Description("保存")
    @ApiOperation(value = "保存")
    public Resp save(@RequestBody LearningExperienceSaveDTO learningExperienceSaveDTO) {
        return learningExperienceService.save(learningExperienceSaveDTO);
    }

    @GetMapping("/{id}")
    @CommonController
    @Description("根据ID查询")
    @ApiOperation(value = "保存")
    public Resp findById(@PathVariable String id){
        return new Resp().success(learningExperienceService.findById(id));
    }

    /**
     * 根据Id删除
     *
     * @param id id
     * @return Resp
     */
    @DeleteMapping("/{id}")
    @CommonController
    @Description("根据id删除")
    @ApiOperation(value = "根据id删除")
    public Resp delete(@ApiParam(value = "id", required = true) @PathVariable("id") String id) {
        LearningExperience learningExperience = learningExperienceService.selectById(id);
        if (learningExperience != null && learningExperience.getIsDeleted() == 2) {
            learningExperience.setIsDeleted(1);
            if (learningExperienceService.updateById(learningExperience)) {
                return new Resp().success();
            }
            return new Resp().failure();
        }
        return new Resp().failure();
    }
}
